package com.ls.atf.core.web.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.ls.atf.core.web.WebPage;
import com.ls.atf.core.web.elements.Link;

public class AdminHomePage extends WebPage<AdminHomePage> {

	private static final String PAGE_URL = BASE_URL + "/admin";

	public AdminHomePage(WebDriver driver) {
		super(driver);
	}

	@Override
	public AdminHomePage load() {
		driver.get(PAGE_URL);
		return this;
	}

	@Override
	public boolean isAvailable() {
		return getManageUsersLink().isAvailable()
				&& getLogoutLink().isAvailable();
	}

	public LoginPage loadAsAnonymousUser() {
		load();
		return new LoginPage(driver).waitUntilAvailable();
	}

	public AdminHomePage loadTestPage() {
		driver.get("http://yahoo.com");
		return this;
	}

	public ManageUsersPage goToManageUsersPage() {
		getManageUsersLink().click();
		return new ManageUsersPage(driver).waitUntilAvailable();
	}

	public LoginPage logoutUser() {
		getLogoutLink().click();
		return new LoginPage(driver).waitUntilAvailable();
	}

	private Link getManageUsersLink() {
		return new Link(driver,
				By.xpath("//a[contains(@href, '/admin_users')]"));
	}

	private Link getLogoutLink() {
		return new Link(driver,
				By.xpath("//a[contains(@href, '/access/logout')]"));
	}

}
